/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargarRankInvitados.java
 *
 * Created on Mar 22, 2011, 9:47:22 PM
 */

package forms;

import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import managers.ManagerPuntajeAmigos;

/**
 *
 * @author nMoncho
 */
public class CargarRankInvitados extends javax.swing.JFrame {
    
    /** Creates new form CargarRankInvitados */
    public CargarRankInvitados() {
        initComponents();
        this.setLocation(200, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePuntajes = new javax.swing.JTable();
        botonCerrar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        botonAgregar = new javax.swing.JButton();
        botonBorrar = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Puntajes por Invitados");
        setIconImage(new ImageIcon(getClass().getResource("/resources/arrow-branch-bgr-16-ns.png")).getImage());

        tablePuntajes.setModel(gestor.getModel());
        tablePuntajes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablePuntajesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tablePuntajes);

        botonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-cross-16-ns.png"))); // NOI18N
        botonCerrar.setText("Cerrar");
        botonCerrar.setToolTipText("Cerrar Ventana");
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-check-16-ns.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guardar los Cambios");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        jLabel1.setText("Listado de Puntajes:");

        botonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/plus-16-ns.png"))); // NOI18N
        botonAgregar.setToolTipText("Agregar Nuevo Puntaje");
        botonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarActionPerformed(evt);
            }
        });

        botonBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/minus-16-ns.png"))); // NOI18N
        botonBorrar.setToolTipText("Borrar un puntaje");
        botonBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botonAgregar, 0, 0, Short.MAX_VALUE)
                                    .addComponent(botonBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(botonCerrar)
                        .addGap(31, 31, 31))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonCerrar, botonGuardar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrar)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonGuardar))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonCerrar, botonGuardar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        if(!tablePuntajes.isValid() || !gestor.isCargaActualValida()){
            JOptionPane.showMessageDialog(this, "Revisa los valores de la tabla, puede que esten incorrectos.\n"
                    + "Si son correctos (no hay celdas en rojo), verifica que todas la celdas esten pintadas de azul,\n"
                    + "haciendo click fuera de la tabla o apretando la tecla TAB.", "Revisar", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
        }else{
            int respuesta = JOptionPane.showConfirmDialog(this, "Los cambios se haran permanentes.\n¿Estas seguro que deseas guardar?", "Confirmacion", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_NO_OPTION);
            if(respuesta == JOptionPane.YES_OPTION){
                boolean exito = gestor.guardarCambios();
                if(exito)
                    JOptionPane.showMessageDialog(this, "Los cambios se guardaron exitosamente", "Exito", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
                else
                    JOptionPane.showMessageDialog(this, "Ocurrio un error tratando de guardar los datos.\nPor favor, revisa lo datos y volve a intentarlo.", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
            }
        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarActionPerformed
        if(!gestor.agregarPuntajeVacio()){
            JOptionPane.showMessageDialog(this, "Ingresaste mal un puntaje. Revisa los siguiente:\n"
                    + "1. El puntaje debe ser mayor a cero\n"
                    + "2. La cantidad maxima debe mayor igual a la cantidad minima\n"
                    + "3. Los rangos de la cantidad de amigos no puede solaparse\n"
                    + "4. Solo un intervalo puede abierto (sin maximo)", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
        }
        tablePuntajes.updateUI();
    }//GEN-LAST:event_botonAgregarActionPerformed

    private void botonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCerrarActionPerformed

    private void tablePuntajesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablePuntajesKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_DELETE){
            borrarFila();
        }
    }//GEN-LAST:event_tablePuntajesKeyPressed

    private void botonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarActionPerformed
        borrarFila();
    }//GEN-LAST:event_botonBorrarActionPerformed

    private void borrarFila(){
        //Preguntar si realmente quiere borrar la fila
        int respuesta = JOptionPane.showConfirmDialog(this, "¿Realmente deseas borrar este/os puntaje/s?", "Advertencia", JOptionPane.YES_NO_OPTION);
        if(respuesta == JOptionPane.YES_OPTION){
            int[] selectedRows = this.tablePuntajes.getSelectedRows();
            int row;
            for(int i=selectedRows.length-1; i>-1;i--){
                row = selectedRows[i];
                gestor.eliminarPuntaje(row);
            }
        }
        tablePuntajes.updateUI();
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargarRankInvitados().setVisible(true);
            }
        });
    }
    private ManagerPuntajeAmigos gestor = new ManagerPuntajeAmigos();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonBorrar;
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablePuntajes;
    // End of variables declaration//GEN-END:variables

}
